package org.origin.ferret.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.origin.centre.model.base.XPage;
import org.origin.centre.model.dto.XDeleteByIdDto;
import org.origin.centre.model.dto.XQueryByIdDto;
import org.origin.centre.model.dto.XUpdateByIdDto;
import org.origin.centre.model.enums.result.RS;
import org.origin.centre.model.result.R;
import org.origin.centre.model.vo.XPageVo;
import org.origin.ferret.system.model.dto.dict.*;
import org.origin.ferret.system.model.vo.dict.SysDictVo;
import org.origin.ferret.system.service.SysDictService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 系统字典-接口
 *
 * @author ferret
 * @version 2024-05-08
 */
@RestController
@Tag(name = "系统字典-接口")
@RequiredArgsConstructor
@RequestMapping("${origin.api:}/sys/dict")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class SysDictController {
    private final SysDictService dictService;

    @PostMapping("page")
    @Operation(summary = "1.分页数据", description = "分页数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:page')")
    public R<XPageVo<SysDictVo>> page(@Valid @RequestBody XPage<SysDictPageDto> pageDto) {
        return R.ok(RS.OK, this.dictService.page(pageDto));
    }

    @GetMapping("query")
    @Operation(summary = "2.查询数据", description = "查询数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:query')")
    public R<SysDictVo> query(@Valid @ParameterObject XQueryByIdDto queryDto) {
        return R.ok(RS.OK, this.dictService.query(queryDto));
    }

    @PutMapping("insert")
    @Operation(summary = "3.插入数据", description = "插入数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:insert')")
    public R<Boolean> insert(@Valid @RequestBody SysDictInsertDto insertDto) {
        return R.ok(RS.OK, this.dictService.insert(insertDto));
    }

    @PostMapping("update")
    @Operation(summary = "4.更新数据", description = "更新数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:update')")
    public R<Boolean> update(@Valid @RequestBody SysDictUpdateDto updateDto) {
        return R.ok(RS.OK, this.dictService.update(updateDto));
    }

    @DeleteMapping("delete")
    @Operation(summary = "5.删除数据", description = "删除数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:delete')")
    public R<Boolean> delete(@Valid @ParameterObject XDeleteByIdDto deleteDto) {
        return R.ok(RS.OK, this.dictService.delete(deleteDto));
    }

    @PostMapping("valid")
    @Operation(summary = "6.生效数据", description = "生效数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:valid')")
    public R<Boolean> valid(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.dictService.valid(updateDto));
    }

    @PostMapping("invalid")
    @Operation(summary = "7.失效数据", description = "失效数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:invalid')")
    public R<Boolean> invalid(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.dictService.invalid(updateDto));
    }

    @PostMapping("/children/page")
    @Operation(summary = "8.分页下级数据", description = "分页下级数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:children:page')")
    public R<XPageVo<SysDictVo>> childrenPage(@Valid @RequestBody XPage<SysDictPageDto> pageDto) {
        return R.ok(RS.OK, this.dictService.childrenPage(pageDto));
    }

    @PutMapping("/children/insert")
    @Operation(summary = "9.插入下级数据", description = "插入下级数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:children:insert')")
    public R<Boolean> childrenInsert(@Valid @RequestBody SysDictChildrenInsertDto insertDto) {
        return R.ok(RS.OK, this.dictService.childrenInsert(insertDto));
    }

    @PostMapping("/children/update")
    @Operation(summary = "10.更新下级数据", description = "更新下级数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:children:update')")
    public R<Boolean> childrenUpdate(@Valid @RequestBody SysDictChildrenUpdateDto updateDto) {
        return R.ok(RS.OK, this.dictService.childrenUpdate(updateDto));
    }

    @DeleteMapping("/children/delete")
    @Operation(summary = "11.删除下级数据", description = "删除下级数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:delete')")
    public R<Boolean> childrenDelete(@Valid @ParameterObject XDeleteByIdDto deleteDto) {
        return R.ok(RS.OK, this.dictService.childrenDelete(deleteDto));
    }

    @PostMapping("/children/valid")
    @Operation(summary = "12.生效下级数据", description = "生效下级数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:valid')")
    public R<Boolean> childrenValid(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.dictService.childrenValid(updateDto));
    }

    @PostMapping("/children/invalid")
    @Operation(summary = "13.失效下级数据", description = "失效下级数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:invalid')")
    public R<Boolean> childrenInvalid(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.dictService.childrenInvalid(updateDto));
    }

    @PostMapping("/children/default")
    @Operation(summary = "14.默认下级数据", description = "默认下级数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:dict:children:invalid')")
    public R<Boolean> childrenDefault(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.dictService.childrenDefault(updateDto));
    }

}
